package com.example.app.ui.Message;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.app.R;
import java.io.File;
import java.util.List;
public class MessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int VIEW_TYPE_MESSAGE_SENT = 1;
    private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
    private static final int VIEW_TYPE_MESSAGE_SENT_IMAGE = 3;
    private static final int VIEW_TYPE_MESSAGE_RECEIVED_IMAGE = 4;
    private Context context;
    private List<ChatMessage> messages;
    private String currentUser; // 当前登录用户
    public MessageAdapter(Context context, List<ChatMessage> messages, String currentUser) {
        this.context = context;
        this.messages = messages;
        this.currentUser = currentUser;
    }
    @Override
    public int getItemViewType(int position) {
        ChatMessage message = messages.get(position);
        if (message.getMessageType() == ChatMessage.TYPE_SENT) {
            return message.getContentType() == ChatMessage.CONTENT_IMAGE ?
                    VIEW_TYPE_MESSAGE_SENT_IMAGE : VIEW_TYPE_MESSAGE_SENT;
        } else {
            return message.getContentType() == ChatMessage.CONTENT_IMAGE ?
                    VIEW_TYPE_MESSAGE_RECEIVED_IMAGE : VIEW_TYPE_MESSAGE_RECEIVED;
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case VIEW_TYPE_MESSAGE_SENT:
                view = LayoutInflater.from(context).inflate(R.layout.item_message_sent, parent, false);
                return new SentMessageHolder(view); // 修改为正确的Holder
            case VIEW_TYPE_MESSAGE_RECEIVED:
                view = LayoutInflater.from(context).inflate(R.layout.item_message_received, parent, false);
                return new ReceivedMessageHolder(view);
            case VIEW_TYPE_MESSAGE_SENT_IMAGE:
                view = LayoutInflater.from(context).inflate(R.layout.item_message_sent_image, parent, false);
                return new SentImageHolder(view); // 使用图片专用Holder
            case VIEW_TYPE_MESSAGE_RECEIVED_IMAGE:
                view = LayoutInflater.from(context).inflate(R.layout.item_message_received_image, parent, false);
                return new ReceivedImageHolder(view); // 使用图片专用Holder
            default:
                return null;
        }
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ChatMessage message = messages.get(position);
        switch (holder.getItemViewType()) {
            case VIEW_TYPE_MESSAGE_SENT:
                ((SentMessageHolder) holder).bind(message);
                break;
            case VIEW_TYPE_MESSAGE_RECEIVED:
                ((ReceivedMessageHolder) holder).bind(message);
                break;
            case VIEW_TYPE_MESSAGE_SENT_IMAGE:
                ((SentImageHolder) holder).bind(message); // 添加图片类型绑定
                break;
            case VIEW_TYPE_MESSAGE_RECEIVED_IMAGE:
                ((ReceivedImageHolder) holder).bind(message); // 添加图片类型绑定
                break;
        }
    }
    @Override
    public int getItemCount() {
        return messages.size();
    }
    private static class SentMessageHolder extends RecyclerView.ViewHolder {
        TextView textView;
        ImageView avatarView; // 新增
        SentMessageHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text_message_body);
            avatarView = itemView.findViewById(R.id.image_avatar); // 新增
        }
        void bind(ChatMessage message) {
            textView.setText(message.getContent());
            avatarView.setImageResource(R.drawable.tx);
        }
    }
    private static class ReceivedMessageHolder extends RecyclerView.ViewHolder {
        TextView textView;
        ReceivedMessageHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text_message_body);
        }
        void bind(ChatMessage message) {
            textView.setText(message.getContent());
        }
    }
    private static class SentImageHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        SentImageHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image_message);
        }
        void bind(ChatMessage message) {
            Glide.with(itemView.getContext()).load(new File(message.getContent())).into(imageView);
        }
    }
    private static class ReceivedImageHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        ImageView avatarView;
        ReceivedImageHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image_message);
            avatarView = itemView.findViewById(R.id.image_avatar);
        }
        void bind(ChatMessage message) {
            Glide.with(itemView.getContext())
                    .load(new File(message.getContent()))
                    .into(imageView);
            avatarView.setImageResource(R.drawable.xm);
        }
    }
}